package cn.bestick.action;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import cn.bestick.dao.UserDao;
import cn.bestick.model.User;
import cn.bestick.util.DbUtil;
import cn.bestick.util.NavUtil;
import cn.bestick.util.ResponseUtil;
import cn.bestick.util.StringUtil;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Bestick
 *
 *         be stick...
 *
 *         2015年8月25日 下午8:11:20
 */
public class UserAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private User user;

	private String error;

	private String imageCode;

	private int userId;

	private String password;

	private String navCode;

	private String mainPage;

	public String getNavCode() {
		return navCode;
	}

	public void setNavCode(String navCode) {
		this.navCode = navCode;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getImageCode() {
		return imageCode;
	}

	public void setImageCode(String imageCode) {
		this.imageCode = imageCode;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	private UserDao userDao = new UserDao();

	public String login() {
		HttpSession session = request.getSession();
		if (StringUtil.isEmpty(user.getUserName())
				|| StringUtil.isEmpty(user.getPassword())) {
			error = "用户名或密码为空！";
			return ERROR;
		}
		if (StringUtil.isEmpty(imageCode)) {
			error = "验证码为空！";
			return ERROR;
		}
		if (!imageCode.equals(session.getAttribute("sRand"))) {
			error = "验证码错误！";
			return ERROR;
		}
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			User currentUser = userDao.login(con, user);
			if (currentUser == null) {
				error = "用户名或密码错误！";
				System.out.println(111);
				return ERROR;
			} else {
				session.setAttribute("currentUser", currentUser);
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}

	public String preSave() {
		navCode = NavUtil.getNavgation("系统管理", "修改密码");
		mainPage = "user/modifyPassword.jsp";
		return SUCCESS;
	}

	public String save() {
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			User user = new User();
			user.setUserId(userId);
			user.setPassword(password);
			int updateNum = userDao.modifyPassword(con, user);
			JSONObject result = new JSONObject();
			if (updateNum > 0) {
				result.put("success", true);
			} else {
				result.put("error", "密码修改失败！");
			}
			ResponseUtil.write(result, ServletActionContext.getResponse());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String logOut() {
		HttpSession session = request.getSession();
		session.removeAttribute("currentUser");
		return "logOut";
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
